//
//  ZYBillListViewController.m
//  ZYOils
//
//  Created by 段新瑞 on 2019/9/30.
//  Copyright © 2019 Mr.Xr. All rights reserved.
//

#import "ZYBillListViewController.h"

#import "ZYHomeBillTableViewCell.h"
#import "ZYChoosePersonPopView.h"

#import "ZZPopWindow.h"
#import "XRRefreshHeader.h"
#import "XRRefreshFooter.h"
#import "ZYBillUserModel.h"
#import "ZYBillInfoMode.h"

@interface ZYBillListViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *chooseNameView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

/// 店员选择 Pop 弹出试图容器
@property (nonatomic) ZZPopWindow *popWindow;

/// 店员选择弹出视图
@property (nonatomic, strong) ZYChoosePersonPopView *choosePersonView;
@property (weak, nonatomic) IBOutlet UIView *topView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (nonatomic, strong) NSMutableArray *nameLists;

@property (nonatomic, strong) NSMutableArray *billLists;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *choosePersonViewLayoutHeight;

@property (nonatomic) NSInteger page;

@end

@implementation ZYBillListViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configViews];
    [self configData];
    
    self.page = 1;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

#pragma mark - Setup View / Data
- (void)configViews {
    self.title = @"账单列表";
    
    if ([ZYUserInfoModel isManager]) {
        self.topView.hidden = NO;
        self.choosePersonViewLayoutHeight.constant = 50;
    } else {
        self.topView.hidden = YES;
        self.choosePersonViewLayoutHeight.constant = 0;
    }
    
    UITapGestureRecognizer *tap = [UITapGestureRecognizer new];
    [self.chooseNameView addGestureRecognizer:tap];
    @weakify(self)
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer *sender) {
        @strongify(self)
        /// 弹出门店选择视图
        self.popWindow = [[ZZPopWindow alloc] init];
        self.popWindow.contentView = self.choosePersonView;
        self.popWindow.popPosition = ZZPopPositionDown;
        self.popWindow.animationSpring = NO;
        self.popWindow.animationIn = 0.25;
        self.popWindow.animationOut = 0.25;
        self.popWindow.showStartY = 64 + IPX_STATUSBAROFFSETHEIGHT + 44;
        self.popWindow.animationType = LBShowAnimationTypeSmooth;
        [self.popWindow showAtView:self.choosePersonView];
    }];
    
    [self.choosePersonView.subject
     subscribeNext:^(ZYBillUserModel *model) {
        @strongify(self);
        [self.popWindow dismiss];
        self.userId = model.userId;
        self.nameLabel.text = model.userName;
        [self getNewData];
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYHomeBillTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZYHomeBillTableViewCell"];
    
    
    self.tableView.mj_header = [XRRefreshHeader headerWithRefreshingBlock:^{
        @strongify(self)
        [self getNewData];
    }];
    
    self.tableView.mj_footer = [XRRefreshFooter footerWithRefreshingBlock:^{
        @strongify(self)
        [self getMoreData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)configData {
    
    /// 获取账单人员
    [ZYHud showLoadingText:@""];
    [[DataManager manager] postGetBillUserParam:@{@"managerId": STR(ZYUserInfoModel.sharedInfoModel.userModel.managerId)} block:^(XRCommonModel *model, NSError *error) {
        [ZYHud hideHud];
        if (model.isSuccess) {
            [self.nameLists addObjectsFromArray:[ZYBillUserModel mj_objectArrayWithKeyValuesArray:model.data]];
            for (ZYBillUserModel *model in self.nameLists) {
                if ([model.userId isEqualToString:self.userId]) {
                    /// 选中 userId 相同用户
                    model.isSelect = YES;
                    self.nameLabel.text = model.userName;
                    break;
                }
            }
            self.choosePersonView.lists = self.nameLists;
            
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}

- (void)getNewData {
    /// 获取账单列表
    NSDictionary *dict = @{@"userId": STR(![LBLimitInput isEmpty:self.userId] ? self.userId : @"0"),
                           @"page": STR_LONG(self.page),
                           @"size": @"10",
                           @"managerId": ZYUserInfoModel.sharedInfoModel.userModel.managerId};
    
    [[DataManager manager] postGetBillInfoParam:dict block:^(XRCommonModel *model, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        if (model.isSuccess) {
            self.page = 1;
            self.billLists = [ZYBillInfoMode mj_objectArrayWithKeyValuesArray:model.data];
            if ([ZYBillInfoMode mj_objectArrayWithKeyValuesArray:model.data].count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer resetNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}

- (void)getMoreData {
    /// 获取账单列表
    NSDictionary *dict = @{@"userId": STR(self.userId != nil ? self.userId : @"0"),
                           @"page": STR_LONG(self.page),
                           @"size": @"10",
                           @"managerId": ZYUserInfoModel.sharedInfoModel.userModel.managerId};
    
    [[DataManager manager] postGetBillInfoParam:dict block:^(XRCommonModel *model, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        if (model.isSuccess) {
            self.page += 1;
            [self.billLists addObjectsFromArray:[ZYBillInfoMode mj_objectArrayWithKeyValuesArray:model.data]];
            if ([ZYBillInfoMode mj_objectArrayWithKeyValuesArray:model.data].count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer resetNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}



#pragma mark - TableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.billLists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYHomeBillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYHomeBillTableViewCell"];
    cell.model = self.billLists[indexPath.row];
    return cell;
}

-  (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

#pragma mark - Observer

#pragma mark - Notification

#pragma mark - Event Response

#pragma mark - Override Methods

#pragma mark - Delegate

#pragma mark - Public Methods

#pragma mark - Private Methods

#pragma mark - Setter / Getter

- (NSMutableArray *)billLists {
    if (!_billLists) {
        _billLists = [[NSMutableArray alloc] init];
    }
    return _billLists;
}


- (NSMutableArray *)nameLists {
    if (!_nameLists) {
        _nameLists = @[].mutableCopy;
    }
    return _nameLists;
}


- (ZYChoosePersonPopView *)choosePersonView {
    if (!_choosePersonView) {
        _choosePersonView = [[ZYChoosePersonPopView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2 - 100, 0, 200, 200)];
        _choosePersonView.clipsToBounds = YES;
        [_choosePersonView acs_radiusWithRadius:10 corner:UIRectCornerBottomLeft | UIRectCornerBottomRight];
    }
    return _choosePersonView;
}

#pragma mark - Network

@end
